package com.hdd.hll.common.config;


import com.hdd.hll.common.interceptor.LoginInterceptor;
import com.github.pagehelper.PageHelper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Properties;


/**
* @Description:  拦截器 和  类的自动注入方法
 */
@Configuration
public class SysConfiguration implements WebMvcConfigurer {

	@Bean
	LoginInterceptor interceptor() {
		return new LoginInterceptor();
	}

	// 配置mybatis的分页插件pageHelper
	@Bean
	public PageHelper pageHelper() {
		PageHelper pageHelper = new PageHelper();
		Properties properties = new Properties();
		properties.setProperty("offsetAsPageNum", "true");
		properties.setProperty("rowBoundsWithCount", "true");
		properties.setProperty("reasonable", "true");
		properties.setProperty("dialect", "mysql"); // 配置mysql数据库的方言
		pageHelper.setProperties(properties);
		return pageHelper;
	}
	

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // addPathPatterns 用于添加拦截规则 ， 先把所有路径都加入拦截， 再一个个排除
    	//不拦截 静态资源、swagger 等 登录相关
        registry.addInterceptor(interceptor())
				.excludePathPatterns("/swagger-ui.html",
						"/swagger-resources/**",
						"/error",
						"/webjars/**"
				).excludePathPatterns("/uploadFile").excludePathPatterns("/uploadMultipleFiles");
    }
	
	
	@Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }


}
